IF OBJECT_ID('dbo.RptPopulateAgreementBudgetTable') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.RptPopulateAgreementBudgetTable
    IF OBJECT_ID('dbo.RptPopulateAgreementBudgetTable') IS NOT NULL
        PRINT '<<< FAILED DROPPING PROCEDURE dbo.RptPopulateAgreementBudgetTable >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE dbo.RptPopulateAgreementBudgetTable >>>'
END
go
-- =======================================================================
-- Procedure Name: dbo.RptPopulateAgreementBudgetTable
--
-- Author:      Kevin Cashman, Parallax Consulting LLC
--
-- Create date: 8/29/2007
--
-- Description: Populates two cached tables..
--             dbo.AgreementBudgets
--             dbo.Agreement_Info
--
-- Parameters:  
--              
--              
-- Returns:     INT, 0=Success, -1=Error
--
-- SampleCall:
-- EXECUTE dbo.RptPopulateAgreementBudgetTable( )
--
--------------------------------------------------------------------------
-- Date        Initials  Modification
--------------------------------------------------------------------------
-- 9/7/07      KC        Created
-- 9/10/07     KC        Added SequenceYear to table
-- 12/28/07    KC        Altered to accomodate changes to destination table
--
-- =======================================================================
CREATE PROCEDURE dbo.RptPopulateAgreementBudgetTable
AS
BEGIN
     -- Initial Settings
     SET NOCOUNT ON
     SET ANSI_WARNINGS OFF
     
     -- Declare Local Variables
     DECLARE   @Error            INT,
               @RowCount         INT

     -- First, Clear target table for Budgets
     TRUNCATE TABLE dbo.AgrAgreement_Budget

     SELECT @Error=@@ERROR,@RowCount=@@ROWCOUNT
     IF @Error!=0
          RETURN -1

     -- Put rows in using View
     INSERT   dbo.AgrAgreement_Budget
              (AgreementId,
              FundID,
              FundNumber,
              BudgetYear,
              SequenceYear,
              IsAdjustment,
              BudgetStartDate,
              BudgetEndDate,
              BudgetViewType,
              BudgetDirectCost,
              BudgetIndirectCost,
              BudgetTotalAmount,
              IndirectCostBasis,
              IndirectCostRate,
              InsertedDate)
     
     SELECT    
              AgreementId,
              FundId,
              FundNumber,
              BudgetYear,
              SequenceYear,
              IsAdjustment,
              BudgetStartDate,
              BudgetEndDate,
              BudgetViewType,
              BudgetDirectCost,
              BudgetIndirectCost,
              BudgetTotalAmount,
              IndirectCostBasis,
              IndirectCostRate,
              GETDATE()
     FROM
              dbo.vwAgreementBudgets  

     SELECT @Error=@@ERROR,@RowCount=@@ROWCOUNT
     IF @Error!=0
          RETURN -1

     -- Update Sequence Year (needed for getting Effort)
     -- NO LONGER NEEDED. Getting from AgrBudget_Fct table
                           
     -- Return Success
     RETURN 0
     
END
go

IF OBJECT_ID('dbo.RptPopulateAgreementBudgetTable') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE dbo.RptPopulateAgreementBudgetTable >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE dbo.RptPopulateAgreementBudgetTable >>>'
go

GRANT EXECUTE ON dbo.RptPopulateAgreementBudgetTable TO InsightReport,Insight
go